package com.xkstream.client.presenter.kt

import com.xkstream.client.api.kt.MessageApi
import com.xkstream.client.api.kt.MessageService
import com.xkstream.client.base.BaseData
import com.xkstream.client.base.BasePresenterImpl
import com.xkstream.client.entity.kt.MessagesData
import com.xkstream.client.rx.RxHelper
import com.xkstream.client.rx.RxObserver

/**
 * Created by xiaoyi on 2017-11-2.
 */
class MessagePresenter() : BasePresenterImpl(), MessageContract.Presenter {


    var view: MessageContract.View? = null
    var messageService: MessageService? = null

    constructor(view: MessageContract.View) : this() {
        this.view = view
        this.view?.setPresenter(this)
        this.messageService = MessageApi.getInstance().service
    }


    override fun getMessageList(params: Map<String, String>) {
        view?.showLoading()
        messageService?.getMessageList(params)
                ?.compose(RxHelper.toUI())
                ?.subscribe(object : RxObserver<BaseData<MessagesData>>(this) {
                    override fun onFail(message: String) {
                        view?.hideLoading()
                        view?.showError(message)
                    }

                    override fun onSuccess(data: BaseData<MessagesData>) {
                        view?.hideLoading()
                        view?.showMessageList(data)
                    }

                })
    }

    override fun readMessage(params: Map<String, String>) {
        view?.showLoading()
        messageService?.readMessage(params)
                ?.compose(RxHelper.toUI())
                ?.subscribe(object : RxObserver<BaseData<Any?>>(this) {
                    override fun onSuccess(t: BaseData<Any?>?) {
                        view?.hideLoading()
                    }

                    override fun onFail(message: String) {
                        view?.hideLoading()
                        view?.showError(message)
                    }
                })


    }


}